package com.moredatasource.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/6/29  11:28
 * 描述: 配置两个数据源
 */
@Configuration
public class TwoDataSourceConfig {

    @Primary
    @Bean("dataSourceProperties1")
    @ConfigurationProperties("lcg.datasource1")
    public DataSourceProperties getDatasourceProperties1(){
       return new DataSourceProperties();
    }

    @Bean("dataSourceProperties2")
    @ConfigurationProperties("lcg.datasource2")
    public DataSourceProperties getDatasourceProperties2(){
        return new DataSourceProperties();
    }


    @Bean("dataSource1")
    @Primary
    public DataSource getDataSource1(@Qualifier("dataSourceProperties1") DataSourceProperties dataSourceProperties){
       return dataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
    }

    @Bean("dataSource2")
    public DataSource getDataSource2(@Qualifier("dataSourceProperties2") DataSourceProperties dataSourceProperties){
        return dataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
    }

}
